/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.rental.service.impl;

import me.zhengjie.modules.rental.domain.Rental;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.rental.repository.RentalRepository;
import me.zhengjie.modules.rental.service.RentalService;
import me.zhengjie.modules.rental.service.dto.RentalDto;
import me.zhengjie.modules.rental.service.dto.RentalQueryCriteria;
import me.zhengjie.modules.rental.service.mapstruct.RentalMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
*
* @description 服务实现
* @author maojinhui
* @date 2020-10-29
**/
@Service
@RequiredArgsConstructor
public class RentalServiceImpl implements RentalService {

    private final RentalRepository rentalRepository;
    private final RentalMapper rentalMapper;

    @Override
    public Map<String,Object> queryAll(RentalQueryCriteria criteria, Pageable pageable){
        Page<Rental> page = rentalRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(rentalMapper::toDto));
    }

    @Override
    public List<RentalDto> queryAll(RentalQueryCriteria criteria){
        return rentalMapper.toDto(rentalRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RentalDto findById(Long id) {
        Rental rental = rentalRepository.findById(id).orElseGet(Rental::new);
        ValidationUtil.isNull(rental.getId(),"Rental","id",id);
        return rentalMapper.toDto(rental);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RentalDto create(Rental resources) {
        return rentalMapper.toDto(rentalRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Rental resources) {
        Rental rental = rentalRepository.findById(resources.getId()).orElseGet(Rental::new);
        ValidationUtil.isNull( rental.getId(),"Rental","id",resources.getId());
        rental.copy(resources);
        rentalRepository.save(rental);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            rentalRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RentalDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RentalDto rental : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("发布人id", rental.getUserId());
            map.put("标题", rental.getTitle());
            map.put("联系电话", rental.getTel());
            map.put("租金/月", rental.getRenet());
            map.put("付款方式", rental.getPayType());
            map.put("房屋面积", rental.getArea());
            map.put("房型", rental.getHouseType());
            map.put("房屋朝向", rental.getToward());
            map.put("楼层", rental.getFloor());
            map.put("是否有电梯", rental.getHasLift());
            map.put("1毛坯房；2简装修；3精装修", rental.getDecoration());
            map.put("地址", rental.getAddress());
            map.put(" createTime",  rental.getCreateTime());
            map.put(" updateTime",  rental.getUpdateTime());
            map.put(" createBy",  rental.getCreateBy());
            map.put(" updateBy",  rental.getUpdatedBy());
            map.put("状态：-1待提交，草稿，0待审核；1审核通过；2审核失败;3已过期", rental.getStatus());
            map.put("生效时间", rental.getStartTime());
            map.put("结束时间", rental.getEndTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}